<%@ WebHandler Language="C#" Class="index" %>

using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections;


public class index : IHttpHandler {
    public static double EarthRadiusKm = 6378.137; // WGS-84
    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "text/plain";
        Uri uri = HttpContext.Current.Request.Url;
        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
        var code = 1;
        var msg = "获取成功";
        var time = DateTime.Now;
        double lon =Convert.ToDouble(HttpContext.Current.Request.Params["lon"]);
        double lat =Convert.ToDouble(HttpContext.Current.Request.Params["lat"]);



        using (Entities bll = new Entities())
        {

            var mystores = bll.S_Store.Where(p => p.IsLock !=true).OrderByDescending(xx => xx.SID).Take(10).ToList();//全部.ToList();
            ArrayList liststore = new ArrayList();

            foreach (var gg in mystores)
            {

                ArrayList listgoods = new ArrayList();
                // var mygoods= bll.S_Goods.Where(p=>p.StoreID==gg.SID&&p.GIState!=true).OrderByDescending(xx=>xx.GSort).ToList();//全部.ToList();
                var mygoods = bll.S_Goods.Where(p =>p.StoreID==gg.SID&& p.GIState != false).OrderByDescending(xx => xx.GSort).ToList();//全部.ToList();

                foreach (var good in mygoods)
                {
                    {
                        var GetgoodGimg = _rky.getFirstImgByimg(good.GImg);
                        listgoods.Add(new
                        {
                            Name = good.GName,
                            gid = good.GID,
                            pic = GetgoodGimg,
                            good.GPrice,
                            good.GPrice2,
                            good.G_Introduce,
                            numb = 0,
                        });
                    }
                }

                var GetGimg = _rky.getFirstImgByimg(gg.Simg);

                double distance=0;

                if (!string.IsNullOrEmpty(gg.Slatitude))
                {
                    distance = AccountDistance(Convert.ToDouble(gg.Slatitude), Convert.ToDouble(gg.Slongitude), lat, lon);
                }

                liststore.Add(new
                {
                    Name = gg.Sname,
                    id = gg.SID,
                    pic = GetGimg,
                    slat = gg.Slatitude,
                    slong = gg.Slongitude,

                    iconPath="/images/park.png",
                    latitude=gg.Slatitude,
                    longitude=gg.Slongitude,
                    width= 32,
                    height= 32,
                    numb = 0,
                    listgoods,
                    distance
                });
            }


            var total = mystores.Count();

            context.Response.Write(JsonConvert.SerializeObject(new { code, msg, time,data = liststore }));

        }


        HttpContext.Current.Response.End();
    }



    private double AccountDistance(double lngA, double latA, double lngB, double latB)
    {
        double DistanceLng = 102834.74258026089786013677476285;
        double DistanceLat = 111712.69150641055729984301412873;
        double LngAbs = Math.Abs((lngA - lngB) * DistanceLng);
        double LatAbs = Math.Abs((latA - latB) * DistanceLat);
        return Math.Sqrt((LatAbs * LatAbs + LngAbs * LngAbs));
    }


    /// <summary>
    /// 获取两点(经纬度表示)间的距离
    /// </summary>
    /// <param name="p1Lat">第一点纬度值</param>
    /// <param name="p1Lng">第一点经度值</param>
    /// <param name="p2Lat">第二点纬度值</param>
    /// <param name="p2Lng">第二点经度值</param>
    /// <returns></returns>
    public static double GetDistance(double p1Lat, double p1Lng, double p2Lat, double p2Lng)
    {
        double dLat1InRad = p1Lat * (Math.PI / 180);
        double dLong1InRad = p1Lng * (Math.PI / 180);
        double dLat2InRad = p2Lat * (Math.PI / 180);
        double dLong2InRad = p2Lng * (Math.PI / 180);
        double dLongitude = dLong2InRad - dLong1InRad;
        double dLatitude = dLat2InRad - dLat1InRad;
        double a = Math.Pow(Math.Sin(dLatitude / 2), 2) + Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) * Math.Pow(Math.Sin(dLongitude / 2), 2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        double dDistance = EarthRadiusKm * c;
        return dDistance;
    }

    public bool IsReusable {
        get {
            return false;
        }
    }




}<%@ WebHandler Language="C#" Class="index" %>

using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections;


public class index : IHttpHandler {
    public static double EarthRadiusKm = 6378.137; // WGS-84
    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "text/plain";
        Uri uri = HttpContext.Current.Request.Url;
        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
        var code = 1;
        var msg = "获取成功";
        var time = DateTime.Now;
        double lon =Convert.ToDouble(HttpContext.Current.Request.Params["lon"]);
        double lat =Convert.ToDouble(HttpContext.Current.Request.Params["lat"]);



        using (Entities bll = new Entities())
        {

            var mystores = bll.S_Store.Where(p => p.IsLock !=true).OrderByDescending(xx => xx.SID).Take(10).ToList();//全部.ToList();
            ArrayList liststore = new ArrayList();

            foreach (var gg in mystores)
            {

                ArrayList listgoods = new ArrayList();
                // var mygoods= bll.S_Goods.Where(p=>p.StoreID==gg.SID&&p.GIState!=true).OrderByDescending(xx=>xx.GSort).ToList();//全部.ToList();
                var mygoods = bll.S_Goods.Where(p =>p.StoreID==gg.SID&& p.GIState != false).OrderByDescending(xx => xx.GSort).ToList();//全部.ToList();

                foreach (var good in mygoods)
                {
                    {
                        var GetgoodGimg = _rky.getFirstImgByimg(good.GImg);
                        listgoods.Add(new
                        {
                            Name = good.GName,
                            gid = good.GID,
                            pic = GetgoodGimg,
                            good.GPrice,
                            good.GPrice2,
                            good.G_Introduce,
                            numb = 0,
                        });
                    }
                }

                var GetGimg = _rky.getFirstImgByimg(gg.Simg);

                double distance=0;

                if (!string.IsNullOrEmpty(gg.Slatitude))
                {
                    distance = AccountDistance(Convert.ToDouble(gg.Slatitude), Convert.ToDouble(gg.Slongitude), lat, lon);
                }

                liststore.Add(new
                {
                    Name = gg.Sname,
                    id = gg.SID,
                    pic = GetGimg,
                    slat = gg.Slatitude,
                    slong = gg.Slongitude,

                    iconPath="/images/park.png",
                    latitude=gg.Slatitude,
                    longitude=gg.Slongitude,
                    width= 32,
                    height= 32,
                    numb = 0,
                    listgoods,
                    distance
                });
            }


            var total = mystores.Count();

            context.Response.Write(JsonConvert.SerializeObject(new { code, msg, time,data = liststore }));

        }


        HttpContext.Current.Response.End();
    }



    private double AccountDistance(double lngA, double latA, double lngB, double latB)
    {
        double DistanceLng = 102834.74258026089786013677476285;
        double DistanceLat = 111712.69150641055729984301412873;
        double LngAbs = Math.Abs((lngA - lngB) * DistanceLng);
        double LatAbs = Math.Abs((latA - latB) * DistanceLat);
        return Math.Sqrt((LatAbs * LatAbs + LngAbs * LngAbs));
    }


    /// <summary>
    /// 获取两点(经纬度表示)间的距离
    /// </summary>
    /// <param name="p1Lat">第一点纬度值</param>
    /// <param name="p1Lng">第一点经度值</param>
    /// <param name="p2Lat">第二点纬度值</param>
    /// <param name="p2Lng">第二点经度值</param>
    /// <returns></returns>
    public static double GetDistance(double p1Lat, double p1Lng, double p2Lat, double p2Lng)
    {
        double dLat1InRad = p1Lat * (Math.PI / 180);
        double dLong1InRad = p1Lng * (Math.PI / 180);
        double dLat2InRad = p2Lat * (Math.PI / 180);
        double dLong2InRad = p2Lng * (Math.PI / 180);
        double dLongitude = dLong2InRad - dLong1InRad;
        double dLatitude = dLat2InRad - dLat1InRad;
        double a = Math.Pow(Math.Sin(dLatitude / 2), 2) + Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) * Math.Pow(Math.Sin(dLongitude / 2), 2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        double dDistance = EarthRadiusKm * c;
        return dDistance;
    }

    public bool IsReusable {
        get {
            return false;
        }
    }




}